# -*- coding: utf-8 -*-
import numpy as np
import cv2
from matplotlib import pyplot as plt

def cv_show(img,name,writename):
    cv2.imshow(name,img)
    cv2.imwrite(writename,img)
    cv2.waitKey()
    cv2.destroyAllWindows()
img = cv2.imread('lena.png',0)
print(img.shape)
#上采样
up = cv2.pyrUp(img)
print(up.shape)
#下采样
down = cv2.pyrDown(img)
print(down.shape)
# 先上采样再下采样:和原图已经有差异，因为上采样会丢失眸子额像素点，再下采样再丢失某些点，最后虽然得到的图像和原图像大小相同但已经比原图模糊
up_down= cv2.pyrDown(up)
print(up_down.shape)
# res = np.hstack((img,up_down))
# cv_show(res,'res','up_down.png')#上下采样时得到的图像无法用这种方法显示是因为图像大小不一样，无法同时显示

#显示在一张图片上
plt.subplot(221), plt.imshow(img, cmap='gray')
plt.title('img')
plt.xlim([0,530]),plt.ylim([530,0])
plt.subplot(222), plt.imshow(up, cmap='gray')
plt.title('up')
plt.xlim([0,530]),plt.ylim([530,0])
plt.subplot(223), plt.imshow(down, cmap='gray')
plt.title('down')
plt.xlim([0,530]),plt.ylim([530,0])
plt.subplot(224), plt.imshow(up_down, cmap='gray')
plt.title('up_down')
plt.xlim([0,530]),plt.ylim([530,0])
plt.savefig('Gaussian.png')
plt.show()